package com.jjmmbb.baoSudoku;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

public abstract class BaseActivity extends Activity {
	public Resources res;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		res = getResources();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 按下键盘上返回按钮
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
		}
		return false;
	}

	// 设置夜间模式
	protected void setNightMode() {
		try {
			if (Comm.isNight) {
				findViewById(R.id.page).setBackgroundResource(R.color.bg_night);
			} else {
				findViewById(R.id.page).setBackgroundResource(R.color.bg_comm);
			}
		} catch (Exception ex) {
			// 找不到id
		}
	}

	// ------------------工具-----------------------//
	protected Bitmap getBitmpDrawableScaledByX(int resId) {
		Bitmap bm = BitmapFactory.decodeResource(res, resId);
		if (null != bm) {
			int w = bm.getWidth();
			int h = bm.getHeight();
			int th = (int) (h * Comm.screenWidth / w);
			bm = Bitmap.createScaledBitmap(bm, Comm.screenWidth, th, false);
		}
		return bm;
	}

	protected Bitmap getAssetsBitmpScaledByX(String avName) {
		String path = "pic/" + avName + ".jpg";
		Bitmap bm = getAssetsBitmp(path);
		if (null != bm) {
			int w = bm.getWidth();
			int h = bm.getHeight();
			int th = (int) (h * Comm.screenWidth / w);
			bm = Bitmap.createScaledBitmap(bm, Comm.screenWidth, th, false);
		}
		return bm;
	}

	public Bitmap getAssetsBitmp(String avName) {
		AssetManager assetManager = getAssets();
		Bitmap bm = null;
		try {
			InputStream is = assetManager.open(avName);
			bm = BitmapFactory.decodeStream(is);
		} catch (IOException e) {
			showException(e);
		}
		return bm;

	}

	/**
	 * 检查字符串是否是空对象或空字符串
	 * 
	 * @param str
	 * @return 为空返回true,不为空返回false
	 */
	public boolean isNull(String str) {
		if (null == str || "".equals(str) || "null".equalsIgnoreCase(str)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 通用消息提示
	 * 
	 * @param resId
	 */
	public void toast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	public void toast(int resId) {
		Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
	}

	/**
	 * 新建一个activity打开
	 * 
	 * @param cls
	 */
	public void gotoActivity(Class<?> cls) {
		Intent intent;
		intent = new Intent(this, cls);
		intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		startActivity(intent);
	}

	public double getDouble(EditText v) {
		if (!"".equals(v.getText().toString())) {
			return Double.valueOf(v.getText().toString());
		}
		return 0d;
	}

	/**
	 * 通用异常抛出,浮出一个通用的提示信息,通常抛出异常掉这个方法就可以了
	 * 
	 * @param msg
	 *            为空时使用默认提示
	 */
	public void showException(Exception e) {
		if (Comm.debug) {
			e.printStackTrace(); // 控制台打印
		}
		toast(R.string.des_exception);
	}

	/**
	 * 从资源获取字符串
	 * 
	 * @param resId
	 * @return
	 */
	public String getStr(int resId) {
		return res.getString(resId);
	}

	public String getString(EditText v) {
		return v.getText().toString();
	}

	public String formatYesNo(boolean yesOrNo) {
		return yesOrNo ? "是" : "否";
	}

	public void back() {
		this.finish();
	}

}
